Codeigniter: how do I select count when `$query->num_rows()` doesn't work for me?

Posted by mOrloff on Stack Overflow See other posts from Stack Overflow or by mOrloff
Published on 2012-10-29T20:39:55Z Indexed on 2012/10/30 5:01 UTC
Read the original article Hit count: 100

Filed under:
|
|
|

I have a query which is returning a sum, so naturally it returns one row.
I need to count the number of records in the DB which made that sum.

Here's a sample of the type of query I am talking about (MySQL):

SELECT 
    i.id, 
    i.vendor_quote_id, 
    i.product_id_requested, 
    SUM(i.quantity_on_hand) AS qty, 
    COUNT(i.quantity_on_hand) AS count
FROM vendor_quote_item AS i
JOIN vendor_quote_container AS c 
    ON i.vendor_quote_id = c.id
LEFT JOIN company_types ON company_types.company_id = c.company_id
WHERE company_types.company_type =  'f'
AND i.product_id_requested =  12345678

I have found and am now using the select_min(), select_max(), and select_sum() functions, but my COUNT() is still hard-coded in.

The main problem is that I am having to specify the table name in a tightly coupled manner with something like $this->$db->select( 'COUNT(myDbPrefix_vendor_quote_item.quantity_on_hand) AS count' ) which kills portability and makes switching environments a PIA.

How can/should I get my the count values I am after with CI in an uncoupled way??

© Stack Overflow or respective owner

Related posts about php

Related posts about codeigniter